﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Himall.Web.Framework.ValidationEx
{
    /// <summary>
    /// 设置佣金比例之和判断
    /// </summary>
    public class SumCommisRateAttribute : ValidationAttribute
    {
        private double _MaxCommisRate;
        private string _CommisRate;
        private string _CommisRateOne;
        private string _CommisRateTwo;
        private string _CommisRateThree;

       

        public double MaxCommisRate
        {
            get
            {
                return _MaxCommisRate;
            }

            set
            {
                _MaxCommisRate = value;
            }
        }

        public string CommisRate
        {
            get
            {
                return _CommisRate;
            }

            set
            {
                _CommisRate = value;
            }
        }

        public string CommisRateOne
        {
            get
            {
                return _CommisRateOne;
            }

            set
            {
                _CommisRateOne = value;
            }
        }

        public string CommisRateTwo
        {
            get
            {
                return _CommisRateTwo;
            }

            set
            {
                _CommisRateTwo = value;
            }
        }

        public string CommisRateThree
        {
            get
            {
                return _CommisRateThree;
            }

            set
            {
                _CommisRateThree = value;
            }
        }

        public SumCommisRateAttribute()
        {            
        }
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {

            // the the  property
            var commisRate = validationContext.ObjectType.GetProperty(CommisRate);
            var commisRatOnee = validationContext.ObjectType.GetProperty(CommisRateOne);
            var commisRateTwo = validationContext.ObjectType.GetProperty(CommisRateTwo);
            var commisRateTree = validationContext.ObjectType.GetProperty(CommisRateThree);

            if (commisRate == null)
                return new ValidationResult(String.Format("Unknown property: {0}.", CommisRate));
            if (commisRatOnee == null)
                return new ValidationResult(String.Format("Unknown property: {0}.", CommisRateOne));
            if (commisRateTwo == null)
                return new ValidationResult(String.Format("Unknown property: {0}.", CommisRateTwo));
            if (commisRateTree == null)
                return new ValidationResult(String.Format("Unknown property: {0}.", CommisRateThree));

            // get the other value

            var r = double.Parse(commisRate.GetValue(validationContext.ObjectInstance, null).ToString());
            var r1 = double.Parse(commisRatOnee.GetValue(validationContext.ObjectInstance, null).ToString());
            var r2 = double.Parse(commisRateTwo.GetValue(validationContext.ObjectInstance, null).ToString());
            var r3 = double.Parse(commisRateTree.GetValue(validationContext.ObjectInstance, null).ToString());
            if ((r + r1 + r2 + r3) >= MaxCommisRate)
            {
                return new ValidationResult(string.Format("佣金比例之和必须小于{0}",MaxCommisRate));
            }
            else {
                return null;
            }

        }

    }
}

